﻿package com.tomvian.twodee.sideon.actions 
{
	import flash.utils.setTimeout;
	import com.tomvian.twodee.common.actions.Action;
	import com.tomvian.twodee.common.Actor;
	import com.tomvian.twodee.common.interactions.Interaction;
	
	/**
	 * Dashing
	 * @author Thomas Vian
	 */
	public class Dashing extends Action
	{
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		public static const DISPLAY_DASHING:String = "dashing";
		
		public var distance:Number;
		public var time:uint;
		
		public var obstructions:Array;
		
		//--------------------------------------------------------------------------
		//
		//  Getters / Setters
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		
		public function Dashing(actor:Actor, distance:Number, time:uint, obstructions:Array) 
		{
			super(actor);
			
			this.distance = distance;
			this.time = time;
			
			this.obstructions = obstructions;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		override public function check(state:uint):void 
		{
			if (actorController.keyPress.dash)
			{
				var obstructed:Boolean = false;
				for (var i:uint = 0, l:uint = obstructions.length; i < l; i++)
				{
					if (Interaction(obstructions[i]).interacting) 
					{
						obstructed = true;
						break;
					}
				}
				
				if (!obstructed)
				{
					actor.currentAction = this;
				}
			}
		}
		
		override public function start():void 
		{
			if (actor.direction == 1)
			{
				actorMass.position.x += distance;
			}
			else if (actor.direction == 3)
			{
				actorMass.position.x -= distance;
			}
			
			setTimeout(finish, time);
		}
		
		protected function finish():void
		{
			actor.returnToDefault();
		}
		
		override public function display():void 
		{
			actorState.state = DISPLAY_DASHING;
		}
	}
}